package com.dragee.modules.work.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.work.dao.ApplyScoreDao;
import com.dragee.modules.work.entity.ApplyScoreEntity;
import com.dragee.modules.work.service.ApplyScoreService;
import com.dragee.utils.StringUtils;
import com.dragee.utils.WrapperUtil;
import com.dragee.utils.excel.CommonListener;
import com.dragee.utils.excel.ExcelUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("applyScoreService")
public class ApplyScoreServiceImpl extends ServiceImpl<ApplyScoreDao, ApplyScoreEntity> implements ApplyScoreService {


    @Resource
    private ApplyScoreDao applyScoreDao;

    private String business = "applyScore";
    /**
    * 列表
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int limit = params.get(CommonConstants.LIMIT) == null
                ?CommonConstants.LIMIT_SIZE:Integer.parseInt(params.get(CommonConstants.LIMIT).toString());
        int start = params.get(CommonConstants.START) == null
                ?CommonConstants.START_SIZE:Integer.parseInt(params.get(CommonConstants.START).toString());
        QueryWrapper<ApplyScoreEntity> wrapper =
                WrapperUtil.getWrapper(business, params, ApplyScoreEntity.class);
        IPage<ApplyScoreEntity> page = new Page<>(start, limit);
        page = baseMapper.selectPage(page, wrapper.select());
        return new PageUtils(page);
    }


    @Override
    public R importData(MultipartFile file) throws IOException {
        //填充对应词典 sex: (1:男, 2:女)
        Map<String, Map<String,Object>> map = new HashMap<>();
        //解析器
        CommonListener<ApplyScoreEntity> commonListener
                = new CommonListener<>(map, ApplyScoreEntity.class,this::excelBatch);
        EasyExcel.read(file.getInputStream())
                .head(ApplyScoreEntity.class)
                .registerReadListener(commonListener)
                .sheet()
                .doRead();
        List<String> msgList = commonListener.getMsgList();
        if(msgList.size()>0){
            return R.error(StringUtils.join(msgList,","));
        }
        return R.ok();
    }

    @Override
    public void exportData(Map<String, Object> params, HttpServletResponse response) throws Exception {
        QueryWrapper<ApplyScoreEntity> wrapper =
                WrapperUtil.getWrapper(business, params, ApplyScoreEntity.class);
        List<ApplyScoreEntity> datas = list(wrapper);
        ExcelUtils.writeExcel(response,
                datas,
                "分数" + System.currentTimeMillis(),
                "数据", ApplyScoreEntity.class);
    }

    /** 导入执行入库方法
     * @param datas
     * @return
     */
    private boolean excelBatch (List<ApplyScoreEntity> datas) {
        for (ApplyScoreEntity data : datas) {
            saveOrUpdate(data);
        }
        return true;
    }

}